<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>用户信息</title>
  <link rel="stylesheet" href="/layui/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/layui/layuiadmin/style/admin.css" media="all">
  <link rel="stylesheet" href="/js/css/clearcss.css" media="all">
  <script type="text/javascript" src="/js/js/clearjs.js"></script>
  <style>
    #addUserModel{
      padding:20px;
      display: none;
    }
    #addUnityUserModel{
      padding:20px;
      display: none;
    }
    #checkUserModel{
      padding:20px;
      display: none;
    }
    #editRoleModel{
      padding:20px;
      display: none;
    }
    .layui-card-header{
      height: 58px;
      line-height: 58px;
    }
    .layui-inline .layui-form-label2{
      padding-left: 20px;
      padding-top: 10px;
      padding-bottom: 10px;
      width: 80px;
      font-weight: 400;
      line-height: 20px;
      text-align: right;
    }
    #menuList{
      border: 1px solid #e3e3e3;
      height: 330px;
      overflow: scroll;
    }
    .layui-tree{
      padding:10px;
    }
    .danger{
      color:#FF5722;
      padding-right: 5px;
    }
    #loginModel{
      padding:20px;
      display: none;
    }
    #handOutModel{
      padding:20px;
      display: none;
    }
    #editRoleModel{
      padding:20px;
      display: none;
    }
    #reSetModel{
      padding:20px;
      display: none;
    }
    #aaa {
      width: 5px;
      height: 5px;
      border: none;
      position: absolute;
      float: left;
      z-index: -1;
      margin-top: -30px;
    }
    #bbb {
      width: 5px;
      height: 5px;
      border: none;
      position: absolute;
      float: left;
      z-index: -1;
      margin-top: -30px;
    }
  </style>
</head>
<body>
<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body" style="padding-bottom: 0">
          <div class="layui-form">
            <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label">用户名：</label>
                <div class="layui-input-inline">
                  <input type="text" name="uName" id="uName" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                </div>
              </div>
              <div class="layui-inline">
                <label class="layui-form-label">部门：</label>
                <div class="layui-input-inline">
                  <select name="s_dept" lay-verify="s_dept" lay-search id="s_dept" lay-filter="s_dept">
                  </select>
                </div>
              </div>
              <div class="layui-inline">
                <label class="layui-form-label">角色：</label>
                <div class="layui-input-inline">
                  <select name="uRole" lay-verify="uRole" lay-search id="uRole" lay-filter="uRole">
                  </select>
                </div>
              </div>
              <div class="layui-inline" style="float: right">
                <button class="layui-btn layui-btn-primary layui-btn-sm" lay-active="reload">清空</button>
                <button class="layui-btn layui-btn-sm" lay-event="AllData" data-type="AllData" lay-active="AllData">搜索</button>
              </div>
            </div>
          </div>
        </div>
        <div class="layui-card-body">
          <table id="userList" lay-filter="userList" lay-data="{id:'userList'}"></table>
        </div>
        <button id="inUserExcel" style="display: none"></button>
        <div type="text/html" id="role_list" style="display:none">
          <th:block th:if="${delete==1}">
            <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
          </th:block>
          <th:block th:if="${add==1}">
            <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="addUser">新增用户</a>
            <a class="layui-btn layui-btn-sm" lay-event="ExcelModel" data-type="userExcel" lay-active="userExcel">下载导入模版</a>
            <a class="layui-btn layui-btn-sm" lay-event="importExcel">导入用户信息</a>
            <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="rePass" >密码重置</a>
          </th:block>

          <button class="layui-btn layui-btn-sm" lay-event="userExcel" data-type="userExcel" lay-active="userExcel">导出用户信息</button>
        </div>
        <script type="text/html" id="test-table-operate-barDemo">
          <th:block th:if="${edit==1}">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑/查看</a>
          </th:block>
          <th:block th:if="${edit==0}">
            <a class="layui-btn layui-btn-xs" lay-event="check">查看</a>
          </th:block>
        </script>
        <script type="text/html" id="status">
          <th:block th:if="${edit==1}">
            {{#  if(d.status==1){ }}
            <input type="checkbox" name="status" lay-skin="switch" checked lay-text="正常|禁用" value={{ d.id }}
                   lay-filter="status">
            {{#  } else { }}
            <input type="checkbox" name="status" lay-skin="switch" lay-text="正常|禁用" value={{ d.id }}
                   lay-filter="status">
            {{#  } }}
          </th:block>
          <th:block th:if="${edit==0}">
            {{#  if(d.status==1){ }}
            <input type="checkbox" name="status" lay-skin="switch" checked lay-text="正常|禁用" value={{ d.id }}
                   lay-filter="status" disabled>
            {{#  } else { }}
            <input type="checkbox" name="status" lay-skin="switch" lay-text="正常|禁用" value={{ d.id }}
                   lay-filter="status" disabled>
            {{#  } }}
          </th:block>
        </script>
      </div>
    </div>
  </div>
</div>
<!--添加用户Model addUserModel-->
<div id="addUserModel">
  <form class="layui-form layui-form-pane" action="" lay-filter="formTest1">
    <div class="layui-form-item">
      <label class="layui-form-label"><span class="danger">*</span>用户名</label>
      <div class="layui-input-block">
        <input type="text" name="add_username" id="add_username" lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label"><span class="danger">*</span>角色</label>
      <div class="layui-input-block">
        <select name="add_role" id="add_role" lay-verify="required">
        </select>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label"><span class="danger">*</span>部门</label>
      <div class="layui-input-block">
        <select name="add_dept" id="add_dept" lay-filter="add_dept" lay-search lay-verify="required">
        </select>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">邮箱</label>
      <div class="layui-input-block">
        <input type="text" name="add_phone" id="add_emial" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">电话</label>
      <div class="layui-input-block">
        <input type="text" name="add_phone" id="add_phone" placeholder="请输入电话" autocomplete="off" class="layui-input">
      </div>
    </div>
  </form>
</div>
<!--查看用户Model addUserModel-->
<div id="checkUserModel">
  <form class="layui-form layui-form-pane" action="" lay-filter="formTest2">
    <div class="layui-form-item">
      <label class="layui-form-label">用户名</label>
      <div class="layui-input-block">
        <input type="text" name="c_username" id="c_username" autocomplete="off" class="layui-input" disabled>
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label">角色</label>
      <div class="layui-input-block">
        <input type="text" name="c_role" id="c_role" autocomplete="off" class="layui-input" disabled>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">部门</label>
      <div class="layui-input-block">
        <input type="text" name="c_dept" id="c_dept" autocomplete="off" class="layui-input" disabled>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">邮箱</label>
      <div class="layui-input-block">
        <input type="text" name="c_phone" id="c_email"  autocomplete="off" class="layui-input" disabled>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">电话</label>
      <div class="layui-input-block">
        <input type="text" name="c_phone" id="c_phone"  autocomplete="off" class="layui-input" disabled>
      </div>
    </div>
  </form>
</div>

<script src="/layui/layuiadmin/layui/layui.js"></script>
<script type="text/javascript" src="/js/jquery-3.7.1.min.js"></script>
<script type="text/javascript" src="/js/md5.js"></script>
<script>
  layui.use(['table','layer','form','util','tree','upload'], function(){
    let table = layui.table;
    let layer=layui.layer;
    let form=layui.form;
    let util=layui.util;
    let tree=layui.tree;
    let upload=layui.upload;


    $("#addUnity_dept").empty();
    $.post('/management/OtherUnitsDeptList', {}, function (data) {
      $('#addUnity_dept').append("<option disabled selected value>请选择部门</option>")
      for(let i=0;i<data.length;i++){
        $('#addUnity_dept').append("<option value='"+data[i].value+"'>"+data[i].text+"</option>");
      }
      form.render('select');
    }, 'json');

    $("#s_dept").empty();
    $.post('/management/SearchDeptList?type=allType',function(data){
      $('#s_dept').append("<option disabled selected value>请选择部门</option>")
      for(let i=0;i<data.length;i++){
        $('#s_dept').append("<option value='"+data[i].value+"'>"+data[i].text+"</option>");
      }
      form.render('select');
    },'json');


    $("#add_role").empty();
    $("#uRole").empty();
    $.post('/management/SearchRoleList',function(data){
      $('#add_role').append("<option disabled selected value>请选择角色</option>")
      for(let i=0;i<data.length;i++){
        $('#add_role').append("<option value='"+data[i].value+"'>"+data[i].text+"</option>");
      }
      $('#uRole').append("<option disabled selected value>请选择角色</option>")
      for(let i=0;i<data.length;i++){
        $('#uRole').append("<option value='"+data[i].value+"'>"+data[i].text+"</option>");
      }
      form.render('select');
    },'json');

    $("#edit_role").empty();
    $.post('/management/SearchRoleList',function(data){
      $('#edit_role').append("<option disabled selected value>请选择角色</option>")
      for(let i=0;i<data.length;i++){
        $('#edit_role').append("<option value='"+data[i].value+"'>"+data[i].text+"</option>");
      }
      form.render('select');
    },'json');

    table.render({
      elem: '#userList'
      ,url: '/user/User' //数据接口
      ,page: true //开启分页
      ,limit:15
      ,limits:[15,20,30,50,500]
      ,defaultToolbar: ['filter', 'print']
      ,toolbar:'#role_list'
      ,loading:true
      ,height: 'full-116'
      ,cols: [
        [ //表头
          {type:'checkbox', fixed: 'left'}
          ,{field:'No', width:60, title: '序号', type:'numbers', fixed: 'left'}
          ,{field: 'id', title: 'ID',  hide:'hide'}
          ,{field: 'username', title: '用户名',  sort: true,}
          ,{field: 'role', title: '角色ID',  hide:'hide'}
          ,{field: 'role_name', title: '角色',  sort: true,}
          ,{field: 'deptId', title: '部门ID',  hide:'hide',}
          ,{field: 'deptName', title: '部门',  sort: true,}
          ,{field: 'email', title: '邮箱',  sort: true,}
          ,{field: 'phone', title: '电话号码',  sort: true,}
          ,{field: 'status', title: '状态', width: 120, align: 'center', templet: '#status'}
          ,{align:'center',title:'操作', fixed: 'right', toolbar: '#test-table-operate-barDemo',width:200}
        ]
      ]
    });
    table.on('toolbar(userList)', function(obj){
      let checkStatus = table.checkStatus(obj.config.id);
      switch(obj.event){
        case "addUser":
          layer.open({
            type: 1,
            title:'添加用户',
            area: ['500px', '510px'], //自定义文本域宽高
            content: $('#addUserModel'), //这里content是一个普通的String
            btn: ['提交', '取消'],//这里content是一个普通的String
            btnAlign: 'c',
            cancel: function(index, layero){
              fromClear();
            },
            success: function(layero, index){
              // 添加form标识
              layero.addClass('layui-form');
              // 将保存按钮改变成提交按钮
              layero.find('.layui-layer-btn0').attr({
                'lay-filter': 'testttt',
                'lay-submit': ''
              });
              form.render();
            },
            yes:function(index,layero){
              form.on('submit(testttt)',function(data){
                let add_username=$("#add_username").val()//用户名
                let add_cardNo=$("#add_cardNo").val()//卡号
                let add_role=$("#add_role").val()//角色
                let add_authority=$("#add_authority").val()//权限
                let add_dept=$("#add_dept").val()//部门
                let add_team=$("#add_team").val()//班组
                let add_company=$("#add_company").val()//公司;
                let add_phone=$("#add_phone").val()//电话;
                // if(add_team==''){
                //     if(add_role>=11 || add_authority>=4){
                //         layer.msg("权限和角色为班组或普通用户时，班组为必选项")
                //         return
                //     }
                // }
                if(add_role==null || add_authority==null || add_dept==null || add_company==null){
                  layer.msg("必填项为空，请检查")
                  return
                }
                $.post('/user/addUser', {username:add_username,role:add_role,authority:add_authority,dept:add_dept,pass:hex_md5(encodeURIComponent(add_username+"GZdcd@gj12")),cardNo:add_cardNo,team:add_team,company:add_company,phone:add_phone},function(data){
                  if(data.code=="suss"){
                    layer.closeAll();
                    layer.msg("添加成功")
                    table.reload('userList', {
                      url: '/user/User'
                    });
                    fromClear()
                  }else if(data.code=="1061"){
                    layer.msg("添加失败，越权创建", {
                      icon: 4,
                      time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                  }else if(data.code=="1062"){
                    layer.msg("添加失败,用户名重复", {
                      icon: 4,
                      time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                  }else if(data.code=="1063"){
                    layer.msg("添加失败,卡号重复", {
                      icon: 4,
                      time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                  }else if(data.code=="1064"){
                    layer.msg("用户信息获取失败，请重新登陆", {
                      icon: 4,
                      time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                  }else{
                    layer.msg("添加失败，请联系管理员", {
                      icon: 2,
                      time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                  }
                },'json');
              })
            },
            btn2:function(index,layero){
              fromClear();
            }
          });
          break;
        case "del":
          let lh=checkStatus.data.length
          let ids=[];
          let uids=[];
          let cardIds=[];
          for (let i=0;i<lh;i++){
            ids.push(checkStatus.data[i].id)
            cardIds.push(checkStatus.data[i].cardId)
            uids.push(checkStatus.data[i].uid)
          }
          if(ids.length==0){
            layer.msg("请选择要删除的用户")
          }else{
            layer.confirm('是否删除选中的用户？', function(index){
              $.post('/user/DelUser',{ids:ids.toString(),cardIds:cardIds.toString(),uids:uids.toString()},function(data){
                if(data.code=="suss"){
                  layer.close(index);
                  layer.msg("删除成功")
                  table.reload('userList', {
                    url: '/user/User'
                  });
                }else{
                  layer.msg("删除失败，请联系管理员", {
                    icon: 2,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                  });
                }
              },'json');
              layer.close(index);
              //向服务端发送删除指令
            });
          }
          break;
        case "userExcel":
          let uname=$("#uName").val();
          let uRole=$("#uRole").val();
          let uPower=$("#uPower").val();
          let s_company=$("#s_company").val();
          let s_dept=$("#s_dept").val();
          let s_team=$("#s_team").val();
          let uIndividuation=$("#uIndividuation").val();
          window.open("/user/outUserExcel?uname="+uname+"&uRole="+uRole+"&uPower="+uPower+"&s_company="+s_company+"&s_dept="+s_dept+"&s_team="+s_team+"&uIndividuation"+uIndividuation,"_blank");
          break;
        case "ExcelModel":
          window.open("/user/outUserExcelModel","_blank");
          break;
        case "rePass":
          let lh2=checkStatus.data.length
          let ids2=[];
          let pa2=[];
          let name2=[];
          let roleId=[];
          for (let i=0;i<lh2;i++){
            ids2.push(checkStatus.data[i].id)
            pa2.push(hex_md5(encodeURIComponent(checkStatus.data[i].username+'GZdcd@gj12')))
            name2.push(checkStatus.data[i].username)
            roleId.push(checkStatus.data[i].role)
          }
          if(ids2.length==0){
            layer.msg("请选择要重置密码的用户")
          }else{
            layer.confirm('是否为用户重置密码？', function(index){
              $.post('/user/rePassUser',{ids:ids2.toString(),pass:pa2.toString(),name:name2.toString(),roleId:roleId.toString()},function(data){
                if(data.code=="suss"){
                  layer.close(index);
                  layer.msg("重置成功")
                  table.reload('userList', {
                    url: '/user/User'
                  });
                }else if(data.code=="501"){
                  layer.msg("重置失败，越权操作", {
                    icon: 2,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                  });
                }else if(data.code=="500"){
                  layer.msg("重置失败，未获取用户信息", {
                    icon: 2,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                  });
                }else{
                  layer.msg("重置失败，请联系管理员", {
                    icon: 2,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                  });
                }
              },'json');
              layer.close(index);
              //向服务端发送删除指令
            });
          }
          break;
        case "face":
          layer.open({
            id:'loginWindow',
            type: 1,
            title:'刷卡',
            area: ['550px', '410px'], //自定义文本域宽高
            content: $('#loginModel'),
            btn: ['选择人员','取消'],//这里content是一个普通的String
            btnAlign: 'r',
            cancel: function(index, layero){
              fromClear();
            },
            success: function(layero, index){
              $("#aaa").on('blur',function(){
                $("#aaa").focus();
              })
              $("#aaa").focus();
            },
            yes:function(index,layero){
              $("#inName").empty();
              $.post('/log/getUser',function(data){
                $('#inName').append("<option disabled selected value>请选择人员</option>")
                for(let i=0;i<data.length;i++){
                  $('#inName').append("<option value='"+data[i].value+"'>"+data[i].text+"</option>");
                }
                form.render('select');
              },'json');
              layer.open({
                id:'handOutWindow',
                type: 1,
                title:'请选择用户',
                area: ['405px', '289px'], //自定义文本域宽高
                content: $('#handOutModel'),
                btn: ['确定', '取消'],//这里content是一个普通的String
                btnAlign: 'c',
                cancel: function(index, layero){
                  $("#aaa").focus();
                  fromClear();
                },
                success:function(){
                  $("#aaa").blur();
                },
                yes:function(index,layero){
                  let uid=$("#inName").val()
                  $.post('/user/openFace',{uid:uid},function(data){
                    if(data.code=="suss"){
                      layer.msg("已调用设备，请在设备录入人脸")
                      layer.closeAll();
                    }
                  },'json');
                },
                btn2:function(index,layero){
                  $("#aaa").focus();

                },
              });
            },
            btn2:function(index,layero){
              $("#aaa").blur();
              layer.closeAll();
            }
          });
          break;
        case "fps":
          layer.open({
            id:'loginWindow',
            type: 1,
            title:'刷卡',
            area: ['550px', '410px'], //自定义文本域宽高
            content: $('#loginModel'),
            btn: ['选择人员','取消'],//这里content是一个普通的String
            btnAlign: 'r',
            cancel: function(index, layero){
              fromClear();
            },
            success: function(layero, index){
              $("#bbb").on('blur',function(){
                $("#bbb").focus();
              })
              $("#bbb").focus();
            },
            yes:function(index,layero){
              $("#inName").empty();
              $.post('/log/getUser',function(data){
                $('#inName').append("<option disabled selected value>请选择人员</option>")
                for(let i=0;i<data.length;i++){
                  $('#inName').append("<option value='"+data[i].value+"'>"+data[i].text+"</option>");
                }
                form.render('select');
              },'json');
              layer.open({
                id:'handOutWindow',
                type: 1,
                title:'请选择用户',
                area: ['405px', '289px'], //自定义文本域宽高
                content: $('#handOutModel'),
                btn: ['确定', '取消'],//这里content是一个普通的String
                btnAlign: 'c',
                cancel: function(index, layero){
                  $("#bbb").focus();
                  fromClear();
                },
                success:function(){
                  $("#bbb").blur();
                },
                yes:function(index,layero){
                  let uid=$("#inName").val()
                  $.post('/user/openFps',{uid:uid},function(data){
                    if(data.code=="suss"){
                      layer.msg("已调用设备，请在设备录入指纹")
                      layer.closeAll();
                    }
                  },'json');
                },
                btn2:function(index,layero){
                  $("#bbb").focus();
                },
              });
            },
            btn2:function(index,layero){
              $("#bbb").blur();
              layer.closeAll();
            }
          });
          break;
        case "reSet":
          let lh3=checkStatus.data.length
          let list=[];
          // let ids3=[];
          // let uids3=[];
          for (let i=0;i<lh3;i++){
            // ids3.push(checkStatus.data[i].id)
            // uids3.push(checkStatus.data[i].uid)
            let object = {};
            object.id=checkStatus.data[i].id
            object.uid=checkStatus.data[i].uid
            list.push(object)
          }
          layer.open({
            type: 1,
            title:'重置权限',
            area: ['360px', '320px'], //自定义文本域宽高
            content: $('#reSetModel'), //这里content是一个普通的String
            btn: ['提交', '取消'],//这里content是一个普通的String
            btnAlign: 'c',
            cancel: function(index, layero){
              fromClear();
            },
            success: function(layero, index){
              // 添加form标识
              layero.addClass('layui-form');
              // 将保存按钮改变成提交按钮
              layero.find('.layui-layer-btn0').attr({
                'lay-filter': 'testttt',
                'lay-submit': ''
              });
              form.render();
            },
            yes:function(index,layero){
              form.on('submit(testttt)',function(data){
                let edit_role=$("#edit_role").val()//角色
                let edit_authority=$("#edit_authority").val()//权限
                if(edit_role==null || edit_authority==null){
                  layer.msg("必填项为空，请检查")
                  return
                }
                $.post('/user/reSetUser', {ids:JSON.stringify(list),role:edit_role,authority:edit_authority},function(data){
                  if(data.code=="suss"){
                    layer.closeAll();
                    layer.msg("添加成功")
                    table.reload('userList', {
                      url: '/user/User'
                    });
                    fromClear()
                  }else{
                    layer.msg("添加失败，请联系管理员", {
                      icon: 2,
                      time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                  }
                },'json');
              })
            },
            btn2:function(index,layero){
              fromClear();
            }
          });
          break;
        case "importExcel":
          $("#inUserExcel").click();
          break;
        case "synUser":
          li= layer.load(2);
          $.post('/user/synUser',{},function(data){
            if(data.code=="suss"){
              layer.close(li);
              layer.msg("同步成功")
            }
          },'json');
          break;
      };
    });
    table.on('tool(userList)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
      var data2 = obj.data; //获得当前行数据
      var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
      let Tid=data2.id;
      let Uid=data2.uid;
      if(layEvent === 'edit'){
        form.val("formTest1", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
          "add_username": data2.username // "name": "value"
          ,"add_cardNo":data2.cardNo
          ,"add_role": data2.role
          ,"add_authority": data2.authority
          ,"add_dept": data2.deptId==0?'':data2.deptId
          ,"add_company": data2.companyId==0?'':data2.companyId
          ,"add_phone": data2.phone
        });
        $("#add_dept").empty()
        $.post('/management/SearchDeptList2',{companyId:data2.companyId},function(data){
          $('#add_dept').append("<option disabled value>请选择部门</option>")
          for(let i=0;i<data.length;i++){
            if(data[i].value==data2.deptId){
              $('#add_dept').append("<option id='"+data[i].value+"' value='"+data[i].value+"' selected>"+data[i].text+"</option>");
            }else{
              $('#add_dept').append("<option id='"+data[i].value+"' value='"+data[i].value+"'>"+data[i].text+"</option>");
            }
          }
          form.render('select');
        },'json');
        $("#add_team").empty()
        $.post('/management/getTeamBll',{deptId:data2.deptId},function(data){
          $('#add_team').append("<option disabled selected value>请选择班组</option>")
          for(let i=0;i<data.length;i++){
            if(data[i].value==data2.teamId){
              $('#add_team').append("<option id='"+data[i].value+"' value='"+data[i].value+"' selected>"+data[i].text+"</option>");
            }else{
              $('#add_team').append("<option id='"+data[i].value+"' value='"+data[i].value+"'>"+data[i].text+"</option>");
            }
          }
          form.render('select');
        },'json');
        form.render('select');
        layer.open({
          id:'edSuit',
          type: 1,
          title:'修改用户信息',
          area: ['500px', '480px'], //自定义文本域宽高
          content: $('#addUserModel'),
          btn: ['提交', '取消'],//这里content是一个普通的String
          btnAlign: 'c',
          cancel: function(index, layero){
            fromClear();
          },
          yes:function(index,layero){
            let add_username=$("#add_username").val()//用户名
            let add_cardNo=$("#add_cardNo").val()//卡号
            let add_role=$("#add_role").val()//角色
            let add_authority=$("#add_authority").val()//权限
            let add_company=$("#add_company").val()//权限
            let add_dept=$("#add_dept").val()//部门
            let add_team=$("#add_team").val()//班组
            let add_phone=$("#add_phone").val()//电话
            // if(add_team==''){
            //     if(add_role>=11 || add_authority>=4){
            //         layer.msg("权限和角色为班组或普通用户时，班组为必选项")
            //         return
            //     }
            // }
            var li= layer.load(2);
            $.post('/user/editUser',
                    {Tid:Tid,Uid:Uid,username:add_username,role:add_role,authority:add_authority,dept:add_dept,cardNo:add_cardNo,team:add_team,cardId:data2.cardId,company:add_company,phone:add_phone},function(data){
                      if(data.code=="suss"){
                        layer.closeAll();
                        layer.msg("修改成功")
                        table.reload('userList', {
                          url: '/user/User'
                        });
                        fromClear();
                      }else if(data.code=="1063"){
                        layer.close(li)
                        layer.msg("修改失败,卡号重复", {
                          icon: 4,
                          time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        });
                      }else{
                        layer.close(li)
                        layer.msg("修改失败，请联系管理员", {
                          icon: 2,
                          time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        });
                      }
                    },'json');
          },
          btn2:function(index,layero){
            fromClear();
          },
        });
      }
      else if(layEvent === 'check'){
        form.val("formTest2", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
          "c_username": data2.username // "name": "value"
          ,"c_role": data2.role_name
          ,"c_authority": data2.authority_name
          ,"c_dept": data2.deptName
          ,"c_cardNo":data2.cardNo
          ,"c_company":data2.companyName
          ,"c_team":data2.teamName
        });
        form.render('select');
        layer.open({
          id:'cheSuit',
          type: 1,
          title:'查看用户信息',
          area: ['500px', '360px'], //自定义文本域宽高
          content: $('#checkUserModel'),
          btn: ['取消'],//这里content是一个普通的String
          btnAlign: 'c',
          cancel: function(index, layero){
            fromClear();
          },
        });
      }
      else if(layEvent === 'setting'){
        form.val("formTest1", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
          "editName": data2.username // "name": "value"
        });
        $.post('/user/getMenuList',function(data){
          tree.render({
            elem: '#menuList'  //绑定元素
            ,showCheckbox:true
            ,checkChild: false
            ,data: data
            ,id: 'demoId' //定义索引
          });
          $.post('/user/getUserForQX',{uid:Tid},function(data){
            console.log(data.list)
            let a=data.list.split(",");
            tree.setChecked('demoId', a);
          },'json');
        },'json');
        layer.open({
          id:'edQX',
          type: 1,
          title:'个性化权限设置',
          area: ['500px', '560px'], //自定义文本域宽高
          content: $('#editRoleModel'),
          btn: ['提交', '取消'],//这里content是一个普通的String
          btnAlign: 'c',
          yes:function(index,layero){
            let checkedData = tree.getChecked('demoId'); //获取选中节点的数据
            if(checkedData.length==0){
              layer.msg("请设置权限")
              return
            }
            let a=JSON.stringify(checkedData).match( /(?<=("id":)).*?(?=,)/g).toString();
            var li= layer.load(2);
            $.post('/user/editQX',
                    {Tid:Tid,Tname:data2.username,role:a},function(data){
                      if(data.code=="suss"){
                        layer.closeAll();
                        layer.msg("修改成功")
                      }else{
                        layer.close(li)
                        layer.msg("修改失败，请联系管理员", {
                          icon: 2,
                          time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        });
                      }
                    },'json');
          },
          btn2:function(index,layero){
          },
        });
      }
    });
    util.event('lay-active',{
      reload:function(res){
        location.reload();
      },
      AllData:function(){
        let uname=$("#uName").val();
        let uRole=$("#uRole").val();
        let uPower=$("#uPower").val();
        let uIndividuation=$("#uIndividuation").val();
        let s_company=$("#s_company").val();
        let s_dept=$("#s_dept").val();
        let s_team=$("#s_team").val();
        table.reload('userList', {
          url: '/user/searchUser'
          ,where: {
            uname:uname,
            uRole:uRole,
            uPower:uPower,
            uIndividuation:uIndividuation,
            s_company:s_company,
            s_dept:s_dept,
            s_team:s_team
          } //设定异步数据接口的额外参数
        });
      },
    })
    // 监听开关事件
    form.on('switch(status)', function (data) {
      var status = this.checked ? 1 : 0;
      console.log("status->>>>>>>>>>>>>>", status);
      console.log("data->>>>>>>>>>>>>>", data.value);
      $.post('/user/editUserState', {id:data.value,status:status},function(data){
        if(data.code=="suss"){
          layer.msg("修改成功", {
            time: 2000 //2秒关闭（如果不配置，默认是3秒）
          });
        }else if(data.code=="500"){
          layer.msg("修改失败，未获取到用户信息，请刷新页面后重试", {
            icon: 2,
            time: 2000 //2秒关闭（如果不配置，默认是3秒）
          });
        }else{
          layer.msg("修改失败，请联系管理员", {
            icon: 2,
            time: 2000 //2秒关闭（如果不配置，默认是3秒）
          });
        }
      },'json');
    });
    upload.render({
      elem: '#inUserExcel' //绑定元素
      ,url: '/user/inUserExcel' //上传接口
      ,field:'EX'
      ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
        li= layer.load(2); //上传loading
        //$('#jindutiao').show();
        //element.progress('progressBar', '20%');
      }
      ,accept:'file'
      ,exts:'xlsx|xls'
      ,done: function(res){
        //上传完毕回调
        layer.close(li);
        if (res.toString() == "true"){
          layer.alert("上传成功",function (){
            table.reload('userList', {
              url: '/user/User'
            });
          });
        } else if(res.code=="500"){
          layer.alert("上传失败，请重新登录");
        }else {
          layer.alert(res.msg);
        }
      }
      ,error: function(){
        //请求异常回调
        $('#jindutiao').hide();
        element.progress('progressBar', '0%');
        layer.close(li);
      }
    });
    $("#aaa").keydown(function (e){
      // 当 keyCode 是13时,是回车操作
      if (e.keyCode == 13){
        // 编写业务代码...
        let cardNo=$("#aaa").val()
        $.post('/user/deviceUserLogin',{cardNo:cardNo},function(data){
          $("#aaa").val("")
          if(data.code=="suss"){
            layer.closeAll()
            $("#aaa").blur()
            let uid=data.userId
            $.post('/user/openFace',{uid:uid},function(data){
              if(data.code=="suss"){
                layer.msg("已调用设备，请在设备上录入人脸")
              }
            },'json');
          }else{
            layer.msg("未找到用户")
          }
        },'json');
      }
    })
    $("#bbb").keydown(function (e){
      // 当 keyCode 是13时,是回车操作
      if (e.keyCode == 13){
        // 编写业务代码...
        let cardNo=$("#bbb").val()
        $.post('/user/deviceUserLogin',{cardNo:cardNo},function(data){
          $("#bbb").val("")
          if(data.code=="suss"){
            layer.closeAll()
            $("#bbb").blur()
            let uid=data.userId
            $.post('/user/openFps',{uid:uid},function(data){
              if(data.code=="suss"){
                layer.msg("已调用设备，请在设备录入指纹")
              }
            },'json');
          }else{
            layer.msg("未找到用户")
          }
        },'json');
      }
    })
  })
  function fromClear(){
    $("#add_username").val("")//用户名
    $("#add_cardNo").val("")//卡号
    $("#add_role").val("")//角色
    $("#add_authority").val("")//权限
    $("#add_dept").val("")//部门
    $("#add_team").val("")//班组
    $("#add_company").val("")//公司;

  }
</script>
</body>
</html>
